package com.asia5b.a5mall.member.fragment

import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.asia5b.a5mall.member.R
import com.asia5b.a5mall.member.adapter.CouponListAdapter
import com.asia5b.a5mall.member.databinding.FragmentCouponlistBinding
import com.asia5b.a5mall.middleware.base.SimpleFragment
import com.asia5b.a5mall.middleware.logic.viewmodel.member.CouponListFragmentVM
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.member.CouponModel
import com.asia5b.a5mall.middleware.utils.newEmptyView
import com.asia5b.a5mall.middleware.utils.startShopActivity
import com.scwang.smartrefresh.layout.api.RefreshLayout
import kotlinx.android.synthetic.main.fragment_couponlist.*

class CouponListFragment : SimpleFragment<CouponListFragmentVM, FragmentCouponlistBinding>() {
    override val viewModelClazz: Class<CouponListFragmentVM>
        get() = CouponListFragmentVM::class.java

    override fun getDataBinding(container: ViewGroup?): FragmentCouponlistBinding {
        return FragmentCouponlistBinding.inflate(layoutInflater,container,false)
    }

    val mRecyclerView by lazy { rv_couponlist }
    val mDatas = arrayListOf<CouponModel>()
    val mAdapter by lazy { CouponListAdapter(mDatas) }
    val emptyView by lazy { newEmptyView(mActivity) }

    //state 状态 1：未使用,2:已使用,3:已过期
    var state = 1


    override fun initView() {
        if(arguments!=null) {
            state = requireArguments().getInt("type", 1)
        }
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = mAdapter
        }
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        getDatas(2)
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        getDatas(1)
    }
    override fun initEvent() {
        mSmartRefreshLayout=mDataBinding.autoCouponlist
        mDataBinding.autoCouponlist.setEnableRefresh(false)

        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            val model = mAdapter.getItem(position) as CouponModel
            when (view.id) {
                R.id.tv_couponlist_use_now,
                R.id.tv_couponlist_shopname -> {
                    startShopActivity(mActivity, model.seller_id)
                }
            }
        }
    }

   override fun startObserver()
    {
        mViewModel.getDatasResultLiveData.observe(this, Observer {
            getDatasResult(it)
        })
    }

    override fun lazyFetchData() {
        getDatas(0)
    }

    fun getDatas(refresh: Int) {
        mViewModel.getDatas(refresh, state)

    }

    fun getDatasResult(resultModel: ResultModel<ArrayList<CouponModel>>) {
        if (emptyView.parent == null) {
            mAdapter.setEmptyView(emptyView)
        }
        if (resultModel.page <= 1) {
            mDatas.clear()
        }
        if (!resultModel.data.isNullOrEmpty()) {
            mAdapter.addData(resultModel.data!!)
        }
        mDataBinding.autoCouponlist.setEnableLoadMore(resultModel.page < resultModel.sumPage)
    }


}